package com.brightfuture.chrms.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brightfuture.chrms.common.core.util.R;
import com.brightfuture.chrms.common.log.annotation.SysLog;
import com.brightfuture.chrms.entity.PositionEntity;
import com.brightfuture.chrms.entity.PositionStandardEntity;
import com.brightfuture.chrms.service.PositionService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.brightfuture.chrms.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 岗位信息
 *
 * @author victor
 * @date 2024-02-04 22:31:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/position")
@Tag(description = "position", name = "岗位信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PositionController {

    private final PositionService positionService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param position 岗位信息
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('chrms_position_view')")
    public R getPositionPage(@ParameterObject Page page, @ParameterObject PositionEntity position) {
        LambdaQueryWrapper<PositionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(position.getDeptId()), PositionEntity::getDeptId, position.getDeptId());
        wrapper.like(StrUtil.isNotBlank(position.getName()), PositionEntity::getName, position.getName());
        wrapper.eq(StrUtil.isNotBlank(position.getDscr()), PositionEntity::getDscr, position.getDscr());
        wrapper.eq(StrUtil.isNotBlank(position.getState()), PositionEntity::getState, position.getState());
        // 选中的结束日期大于等于生效日期，选中的开始日期小于等于失效日期，查询有效范围内的数据
//        TODO:        需要进行字符串日期转换
        if (StrUtil.isNotBlank(position.getDateScope())) {
            String[] dateScope = position.getDateScope().split(",");
            wrapper.ge(Objects.nonNull(dateScope[1]), PositionEntity::getBeginDate, dateScope[1]);
            wrapper.le(Objects.nonNull(dateScope[0]), PositionEntity::getEndDate, dateScope[0]);
        }
        return R.ok(positionService.page(page, wrapper));
    }

    /**
     * 通过id查询岗位信息
     *
     * @param positionId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{positionId}")
    @PreAuthorize("@pms.hasPermission('chrms_position_view')")
    public R getById(@PathVariable("positionId") Long positionId) {
        return R.ok(positionService.getByIdDeep(positionId));
    }

    /**
     * 新增岗位信息
     *
     * @param position 岗位信息
     * @return R
     */
    @Operation(summary = "新增岗位信息", description = "新增岗位信息")
    @SysLog("新增岗位信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('chrms_position_add')")
    public R save(@RequestBody PositionEntity position) {
        return R.ok(positionService.saveDeep(position));
    }

    /**
     * 修改岗位信息
     *
     * @param position 岗位信息
     * @return R
     */
    @Operation(summary = "修改岗位信息", description = "修改岗位信息")
    @SysLog("修改岗位信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('chrms_position_edit')")
    public R updateById(@RequestBody PositionEntity position) {
        return R.ok(positionService.updateDeep(position));
    }

    /**
     * 通过id删除岗位信息
     *
     * @param ids positionId列表
     * @return R
     */
    @Operation(summary = "通过id删除岗位信息", description = "通过id删除岗位信息")
    @SysLog("通过id删除岗位信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('chrms_position_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(positionService.removeDeep(ids));
    }

    /**
     * 通过id删除岗位信息子表数据
     *
     * @param ids positionId列表
     * @return R
     */
    @Operation(summary = "通过id删除岗位信息子表数据", description = "通过id删除岗位信息子表数据")
    @SysLog("通过id删除岗位信息子表数据")
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('chrms_position_del')")
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(positionService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     *
     * @param position 查询条件
     * @param ids      导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('chrms_position_export')")
    public List<PositionEntity> export(PositionEntity position, Long[] ids) {
        return positionService.list(Wrappers.lambdaQuery(position).in(ArrayUtil.isNotEmpty(ids), PositionEntity::getPositionId, ids));
    }
}